from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class OrderListPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.order_list_locator = (By.ID, "order-list")
        self.order_status_locator = (By.ID, "order-status")
        self.order_id_locator = (By.ID, "order-id")
        self.order_date_locator = (By.ID, "order-date")
        self.order_total_locator = (By.ID, "order-total")
        self.filter_status_locator = (By.ID, "filter-status")
        self.search_order_locator = (By.ID, "search-order")
        self.next_page_button_locator = (By.ID, "next-page-button")
        self.previous_page_button_locator = (By.ID, "previous-page-button")
        self.page_number_locator = (By.ID, "page-number")

    def navigate(self):
        self.driver.get('http://example.com/orders')

    def filter_orders_by_status(self, status):
        filter_status = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.filter_status_locator)
        )
        filter_status.send_keys(status)

    def search_order(self, order_id):
        search_order = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.search_order_locator)
        )
        search_order.send_keys(order_id)
        search_order.submit()

    def get_order_list(self):
        order_list = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.order_list_locator)
        )
        return order_list

    def get_order_status(self, index=0):
        order_status = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.order_status_locator)
        )[index]
        return order_status.text

    def get_order_id(self, index=0):
        order_id = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.order_id_locator)
        )[index]
        return order_id.text

    def get_order_date(self, index=0):
        order_date = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.order_date_locator)
        )[index]
        return order_date.text

    def get_order_total(self, index=0):
        order_total = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.order_total_locator)
        )[index]
        return order_total.text

    def click_next_page(self):
        next_page_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.next_page_button_locator)
        )
        next_page_button.click()

    def click_previous_page(self):
        previous_page_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.previous_page_button_locator)
        )
        previous_page_button.click()

    def get_current_page_number(self):
        page_number = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.page_number_locator)
        )
        return int(page_number.text)